﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;
using System.Data.SqlClient;
using SMSPanasonic.Business;
namespace SMSPanasonic
{
    class MakeBulkTable
    {
        public static DataTable MakeSmsMoWaitingTable()
        {
            DataTable retTable = new DataTable("SmsMoWaiting");
            retTable.Columns.Add("MoGuid", typeof(Guid));
            retTable.Columns.Add("Subs", typeof(string));
            retTable.Columns.Add("Message", typeof(string));
            retTable.Columns.Add("ShortCode", typeof(string));
            retTable.Columns.Add("ReceivedTime", typeof(DateTime));
            retTable.Columns.Add("Status", typeof(string));
            retTable.Columns.Add("OperatorID", typeof(int));
            retTable.Columns.Add("SmsInGUID", typeof(Guid));
            retTable.Columns.Add("CooperateID", typeof(int));
            retTable.Columns.Add("AgencyID", typeof(int));
            retTable.Columns.Add("RetryOnFailReqTimes", typeof(int));
            // Create an array for DataColumn objects.
            retTable.PrimaryKey = new DataColumn[] { retTable.Columns[0] };

            return retTable;
        }

        public static DataTable MakeSmsMoStatisticTable()
        {
            DataTable retTable = new DataTable("SmsMoStatistic");
            retTable.Columns.Add("MoGuid", typeof(Guid));
            retTable.Columns.Add("Subs", typeof(string));
            retTable.Columns.Add("Message", typeof(string));
            retTable.Columns.Add("ShortCode", typeof(string));
            retTable.Columns.Add("ReceivedTime", typeof(DateTime));
            retTable.Columns.Add("Status", typeof(string));
            retTable.Columns.Add("OperatorID", typeof(int));
            retTable.Columns.Add("SmsInGUID", typeof(Guid));
            retTable.Columns.Add("CooperateID", typeof(int));
            retTable.Columns.Add("AgencyID", typeof(int));
            retTable.Columns.Add("RetryOnFailReqTimes", typeof(int));

            // Create an array for DataColumn objects.
            retTable.PrimaryKey = new DataColumn[] { retTable.Columns[0] };

            return retTable;
        }

        public static DataTable MakeSmsMtWaitingTable()
        {
            DataTable retTable = new DataTable("SmsMtWaiting");
            retTable.Columns.Add("MtGuid", typeof(Guid));
            retTable.Columns.Add("MoGuid", typeof(Guid));
            retTable.Columns.Add("ShortCode", typeof(string));
            retTable.Columns.Add("DestAddr", typeof(string));
            retTable.Columns.Add("OperatorID", typeof(int));
            retTable.Columns.Add("Message", typeof(string));
            retTable.Columns.Add("CooperateID", typeof(int));
            retTable.Columns.Add("Status", typeof(string));
            retTable.Columns.Add("SentTime", typeof(DateTime));
            retTable.Columns.Add("CDRIndicator", typeof(string));
            retTable.Columns.Add("MTType", typeof(string));
            retTable.Columns.Add("RetryOnFailReqTimes", typeof(int));
            retTable.Columns.Add("RetryOnTimeOutReqTimes", typeof(int));
            retTable.Columns.Add("RetryOnFailReqDuration", typeof(long));
            retTable.Columns.Add("RetryOnTimeOutReqDuration", typeof(long));
            retTable.Columns.Add("AgencyID", typeof(int));
            // Create an array for DataColumn objects.
            retTable.PrimaryKey = new DataColumn[] { retTable.Columns[0] };

            return retTable;
        }

        public static DataTable MakeWarrantyWaitingTable()
        {
            DataTable RetTable = new DataTable("pa_WarrantyWaiting");
            RetTable.Columns.Add("WarrantyWattingID", typeof(int));
            RetTable.Columns.Add("ModelCode", typeof(string));
            RetTable.Columns.Add("ProductCode", typeof(string));
            RetTable.Columns.Add("AgencyID", typeof(int));
            RetTable.Columns.Add("CustomerPhone", typeof(string));
            RetTable.Columns.Add("WarrantyPackageCode", typeof(string));
            RetTable.Columns.Add("RegisterDate", typeof(DateTime));
            RetTable.Columns.Add("WarrantyType", typeof(string));
            RetTable.Columns.Add("IsValid", typeof(bool));
            RetTable.Columns.Add("Approved", typeof(bool));
            RetTable.Columns.Add("CreatedDate", typeof(DateTime));
            RetTable.Columns.Add("ModifiedDate", typeof(DateTime));
            RetTable.Columns.Add("RegisteredFrom", typeof(string));
            RetTable.Columns.Add("RegisteredPhone", typeof(string));
            RetTable.Columns.Add("Description", typeof(string));
            return RetTable;
        }

        public static DataTable MakeCustomerPhoneTable()
        {
            DataTable RetTable = new DataTable("pa_CustomerPhone");
            RetTable.Columns.Add("CustomerPhone", typeof(string));
            // Create an array for DataColumn objects.
            RetTable.PrimaryKey = new DataColumn[] { RetTable.Columns[0] };
            return RetTable;
        }

        public static DataTable MakeSmsMtReportTable()
        {
            DataTable RetTable = new DataTable("SmsMtReport");
            RetTable.Columns.Add("MtGuid", typeof(Guid));
            RetTable.Columns.Add("SentTime", typeof(DateTime));
            RetTable.Columns.Add("Status", typeof(string));
            RetTable.Columns.Add("ReportTime", typeof(DateTime));
            RetTable.Columns.Add("ReportStatus", typeof(string));
            RetTable.PrimaryKey = new DataColumn[] { RetTable.Columns[0] };
            return RetTable;
        }

        public static DataTable MakePaSmsStatisticTable()
        {
            DataTable RetTable = new DataTable("pa_SmsStatistic");
            RetTable.Columns.Add("AgencyID", typeof(int));
            RetTable.Columns.Add("Date_Time", typeof(DateTime));
            RetTable.Columns.Add("RegisterCount", typeof(int));
            RetTable.Columns.Add("ExtentCount", typeof(int));
            RetTable.Columns.Add("FailCount", typeof(int));
            RetTable.PrimaryKey = new DataColumn[] { RetTable.Columns[0], RetTable.Columns[1]};
            return RetTable;
        }

        public static DataTable MakePaWarrantyTable()
        {
            DataTable RetTable = new DataTable("pa_Warranty");
            RetTable.Columns.Add("ModelCode", typeof(string));
            RetTable.Columns.Add("ProductCode", typeof(string));
            RetTable.PrimaryKey = new DataColumn[] { RetTable.Columns[0], RetTable.Columns[1] };
            return RetTable;
        }

        public static DataTable MakePaWarrantyDetailTable()
        {
            DataTable RetTable = new DataTable("pa_WarrantyDetail");
            RetTable.Columns.Add("WarrantyDetailID", typeof(int));
            RetTable.Columns.Add("ModelCode", typeof(string));
            RetTable.Columns.Add("ProductCode", typeof(string));
            RetTable.Columns.Add("AgencyID", typeof(int));
            RetTable.Columns.Add("CustomerPhone", typeof(string));
            RetTable.Columns.Add("WarrantyPackageID", typeof(int));
            RetTable.Columns.Add("CreatedDate", typeof(DateTime));
            RetTable.Columns.Add("RegisterDate", typeof(DateTime));
            RetTable.Columns.Add("ConfirmDate", typeof(DateTime));
            RetTable.Columns.Add("ExpiredDate", typeof(DateTime));
            RetTable.Columns.Add("UserID", typeof(int));
            RetTable.Columns.Add("WarrantyType", typeof(string));
            RetTable.Columns.Add("Description", typeof(string));
            RetTable.Columns.Add("RegisteredFrom", typeof(string));
            RetTable.Columns.Add("RegisteredPhone", typeof(string));
            return RetTable;
        }

        public static DataTable MakePaWarrantyStatusTable()
        {
            DataTable RetTable = new DataTable("pa_Warranty_WarrantyStatus");
            RetTable.Columns.Add("WarrantyStatusID", typeof(int));
            RetTable.Columns.Add("WarrantyDetailID", typeof(int));
            RetTable.PrimaryKey = new DataColumn[] { RetTable.Columns[0], RetTable.Columns[1] };
            return RetTable;
        }
    }
    public class SmSStatus
    {
        public const string Editing = "EDITING";
        public const string WaitingAprrove = "WAITINGAPPROVE";
        public const string Processing = "PROCESSING";
        public const string PendingProcessing = "PENDINGPROCESSING";
        public const string Pending = "PENDING";
        public const string Sent = "SENT";
        public const string Fail = "FAIL";
        public const string RetryOnFail = "RETRYONFAIL";
        public const string SentGateway = "SENTGATEWAY";
        public const string RetryOnFailProcessing = "FAILPROCESSING";
        public const string TimeOut = "TIMEOUT";
        public const string RetryOnTimeOut = "RETRYONTIMEOUT";
        public const string RetryOnTimeOutProcessing = "TIMEOUTPROCESSING";
    }

    class Common
    {
        public static void WriteLog(Exception ex, string log_type)
        {
            Logs logs = new Logs();
            logs.LogSource = ex.Source;
            logs.LogMessage = ex.Message;
            logs.LogTime = DateTime.Now;
            logs.LogTrace = ex.StackTrace;
            logs.LogType = log_type;
            logs.Status = new System.Data.SqlTypes.SqlBoolean(true);
            logs.Save();
        }

        public static void AlertLog(Exception ex, string log_type)
        {

        }

        public static bool WebSerivceIsAvailable(string Url)
        {
            string Message = string.Empty;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);

            // Set the credentials to the current user account
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Method = "GET";

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    // Do nothing; we're only testing to see if we can get the response
                }
            }
            catch (WebException ex)
            {
                Message += ((Message.Length > 0) ? "\n" : "") + ex.Message;
            }

            return (Message.Length == 0);
        }

        public static bool WebSerivceIsAvailable(SmsRouter smsRouter, ref string url_alive)
        {
            url_alive = smsRouter.RouterURL.IsNull ? "" : smsRouter.RouterURL.Value;
            List<string> WebServiceUrlArray = new List<string>();
            if (!smsRouter.RouterURL.IsNull)
                WebServiceUrlArray.Add(smsRouter.RouterURL.Value);
            if (!smsRouter.RouterURL2.IsNull && smsRouter.RouterURL2.Value != "")
            {
                foreach (string s in smsRouter.RouterURL2.Value.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    WebServiceUrlArray.Add(s);
                }
            }
            //Check Net, neu dia chi Die thi chuyen sang dia chi moi
            bool urlRouterIsAlive = false;
            foreach (string url in WebServiceUrlArray)
            {
                if (Common.WebSerivceIsAvailable(url))
                {
                    url_alive = url;
                    urlRouterIsAlive = true;
                    break;
                }
            }
            return urlRouterIsAlive;
        }

        public static ConnectionState isDatabaseAlive(string connectionString)
        {
            ConnectionState conn_state = ConnectionState.Closed;
            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    conn_state = connection.State;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(DateTime.Now.ToString() + ": ErrorMessage:" + ex.Message);
            }
            return conn_state;
        }
    }
}
